using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using Giraffe.Client.Common.WindowsController;
using Giraffe.Core.Common;
using Giraffe.Core.Common.Messengers;
using Giraffe.Core.ICQ;
using Giraffe.Core.Model;
using Giraffe.Mocks.Messenger;
using Microsoft.Practices.Composite.Logging;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Presentation.Regions;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.UnityExtensions;
using Giraffe.Client.Common;
using Microsoft.Practices.Unity;

namespace Giraffe.Client.Shell
{
    public class BootStrapper : UnityBootstrapper
    {
        private TextLogger loggerFacade = new TextLogger(new StreamWriter("log.txt"));

        protected override void ConfigureContainer()
        {
            
            base.ConfigureContainer();
            ObjectProvider.Container = Container;
            //ConfigurePolicyInjection();

            ObjectProvider.RegisterInstance<ILoggerFacade>(loggerFacade); //TODO Write custom logger

            ObjectProvider.RegisterInstance<IApplicationProxy>(new ApplicationProxy());

            ObjectProvider.RegisterInstance<IWindowsController>(new WindowsController());

            var messengers = new List<IMessenger>();
            messengers.Add(ObjectProvider.Resolve<IcqMessenger>());
            //messengers.Add(new MockMessenger());

            var user = new User(messengers);
            ObjectProvider.RegisterInstance<IUser>(user);
            ObjectProvider.RegisterInstance<IConversationManager>(ObjectProvider.Resolve<ConversationManager>());
        }

        //private void ConfigurePolicyInjection()
        //{
            
        //    Container.AddNewExtension<Interception>();
        //    Container
        //        .Configure<Interception>()
        //        .AddPolicy("MethodLogging")
        //        .AddMatchingRule(new NamespaceMatchingRule("Giraffe.*"))
        //        .AddCallHandler(typeof(MethodLoggingCallHandler));
        //}

        protected override ILoggerFacade LoggerFacade
        {
            get
            {
                return loggerFacade;
            }
        }

        protected override DependencyObject CreateShell()
        {
            Shell shell = new Shell();
            shell.DataContext = new ShellViewModel();
            shell.Show();
            return shell;
        }

        protected override IModuleCatalog GetModuleCatalog()
        {
            ModuleCatalog catalog = new ModuleCatalog()
                .AddModule(typeof(MainWindowModule.MainWindowModule))
                .AddModule(typeof(ConversationModule.ConversationModule));
            return catalog;
        }
    }
}
